﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product.RateFactorConverter
{
    public class PaymentPeriodConverter : AbstractRateFactorConverter
    {
        private static readonly string[] alias = new string[] { "付款期", "付款期限", "缴费期", "缴费期限", "交费期", "交费期限", "缴费年限", "付款年限" };

        private static readonly string onceTemplate = @"趸交|趸缴|一次性交费|一次交费|一次交清";

        private static readonly string periodTemplate = @"(\d{1,3})[年月]期?[交缴]?";

        private static readonly string ageTemplate = @"[到至]?(\d{1,3})周?岁";

        private static readonly string equalInsPeriodTemplate = "等于保险期限";

        private static readonly string equalBasicTemplate = "等于主险";

        private static readonly string lifetimeTemplate = "终身|终生|每年";

        public override string[] Alias
        {
            get { return alias; }
        }

        public static IPaymentPeriod CreateProperty(string item)
        {
            IPaymentPeriod pp = new PaymentPeriod();

            Match m = Regex.Match(item, onceTemplate);
            if (m.Success)
            {

                pp.Style = PaymentPeriodStyle.Once;
                pp.Value = 1;
                pp.Name = item;

                return pp;

            }

            m = Regex.Match(item, periodTemplate);

            if (m.Success)
            {
                pp.Style = PaymentPeriodStyle.Period;
                pp.Value = int.Parse(m.Groups[1].Value);
                pp.Name = item;

                return pp;

            }
            m = Regex.Match(item, ageTemplate);

            if (m.Success)
            {
                pp.Style = PaymentPeriodStyle.Age;
                pp.Value = int.Parse(m.Groups[1].Value);
                pp.Name = item;

                return pp;

            }

            if (item == equalInsPeriodTemplate)
            {
                pp.Style = PaymentPeriodStyle.EqualInsPeriod;
                pp.Value = 0;
                pp.Name = item;

                return pp;
            }

            if (item == equalBasicTemplate)
            {
                pp.Style =PaymentPeriodStyle.EqualBasic;
                pp.Value = 0;
                pp.Name = item;

                return pp;
            }

            m = Regex.Match(item, lifetimeTemplate);
            if (m.Success)
            {
                pp.Style = PaymentPeriodStyle.Lifetime;
                pp.Value = 0;
                pp.Name = item;
                return pp;
            }

            throw new Exception(string.Format("{0}:无法匹配付款期限", item));
        }

        public static void CreateProperties(IProduct product,string[] items)
        {
            //product.PaymentPeriods.Clear();

            string[] s = items.GroupBy(p => p).Select(p2 => p2.Key).ToArray<string>();

            foreach (string v in s)
            {
                if (!string.IsNullOrEmpty(v))
                {
                    IPaymentPeriod pp = CreateProperty(v);
                    pp.Tag = true;
                    pp.Owner = product;
                    //product.PaymentPeriods.Add(pp);
                    IPaymentPeriod opp = product.PaymentPeriods.FirstOrDefault(p => p.Style == pp.Style && p.Value == pp.Value);

                    if (opp != null)
                    {
                        opp.Name = pp.Name;
                        opp.Tag = pp.Tag;
                    }
                    else
                    {
                        product.PaymentPeriods.Add(pp);
                    }
                }
                    
            }

            foreach (IPaymentPeriod ipp in product.PaymentPeriods)
            {
                if (ipp.Tag == null)
                {
                    product.PaymentPeriods.Remove(ipp);
                }
            }
        }

        //protected override void AddProperty(string item)
        //{
        //    Match m = Regex.Match(item, onceTemplate);

        //    IProduct product = Owner.Owner;

        //    IPaymentPeriod pp = CreateProperty(item);
        //    pp.Owner = product;

        //    product.PaymentPeriods.Add(pp);
        //}

        public override bool IsToPropertiesEnable { get { return true; } }

        public override void ToProperties()
        {
            CreateProperties(Owner.Owner, Owner.Items.Split(';'));
            Owner.Owner.IsPaymentPeriodsFromFixRate = true;

        }

        public override bool IsPolicySelectedEnable { get { return true; } }

        public override void ToPolicySelected(IPolicy policy,string item)
        {
            policy.SelectedPaymentPeriod = policy.Product.PaymentPeriods.First(p => p.Name == item);
        }

        public override string GetItemByPolicy(IPolicy policy)
        {
            string item = base.GetItemByPolicy(policy);

            if (string.IsNullOrEmpty(item) && policy.SelectedPaymentPeriod !=null)
            {
                item = policy.SelectedPaymentPeriod.Name;
            }

            return item;
        }
    }
}
